#!/bin/sh

set -e

mnemonic=`mktemp`
echo -n 'neutral abandon month park disease forum engage dutch coconut base morning icon wide stock coast fork girl fish despair kiss dilemma pass slide major' > $mnemonic

# Use a custom key path just in case the host is using original one.
tempdir=`mktemp -d`
keypath=$tempdir/key.priv
export BNSCLI_PRIV_KEY=$keypath

bnscli keygen < $mnemonic
rm $mnemonic

# Key generation in non deterministic (cryptography 101) so we cannot compare
# its value. We can only ensure that enough bytes was generated.
# xargs removes the leading whitespaces on OSX
echo "generated private key length: `wc -c < $keypath | xargs`"

# Generating a key when one already exist must fail.
if bnscli keygen 2> /dev/null
then
	>&2 echo "Generation of a private key when file exists must fail."
	exit 1
fi

rm -r $tempdir
